﻿using Aliyun.OSS;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace store.cyt369.com.dal
{
    public class StoreService:store.cyt369.com.dal.DbContexts.StoreBaseService<StoreService>
    {
        /// <summary>
        /// 获取所有的店铺
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IQueryable<store.cyt369.com.models.Store> GetStore(int page, int rows, ref int total, string searchStr = null, int? categoryID = null, int? area = null, int? cityID = null)
        {
            using (StoreCtx)
            {
                var items = StoreCtx.Stores
                    .Include(b => b.StoreImages)
                    .Include(cb => cb.StoreCategory)
                    .ThenInclude(c => c.Parent)
                    .Where(b => b.IsDeleted == false)
                    .AsQueryable();

                if (categoryID.HasValue && categoryID != 0)
                {
                    items = items.Where(i => i.StoreCategoryID == categoryID || i.StoreCategory.ParentID == categoryID);
                }
                if (area.HasValue && area != 0)
                {
                    items = items.Where(i => i.Area == area);
                }
                if (!string.IsNullOrEmpty(searchStr))
                {
                    items = items.Where(c => c.Name != null && c.Name.Contains(searchStr.Trim()));
                }
                if (cityID.HasValue && cityID != 0)
                {
                    items = items.Where(i => i.CityID == cityID);
                }
                return items = this.Pagination(page, rows, ref total, items, c => c.Id);
            }
        }

        /// <summary>
        /// 获取所有的品牌 不包含图片
        /// </summary>
        /// <param name="total"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public IQueryable<store.cyt369.com.models.Store> GetStores_NoIncludeImg(string searchStr = null, int? categoryID = null, int? area = null, int? cityID = null)
        {
            using (StoreCtx)
            {
                var items = StoreCtx.Stores
                    .Include(cb => cb.StoreCategory)
                    .ThenInclude(c => c.Parent)
                    .Where(b => b.IsDeleted == false)
                    .AsQueryable();

        //        if (categoryID.HasValue && categoryID != 0)
        //        {
        //            items = items.Where(i => i.Category.Id == categoryID || i.Category.ParentID == categoryID);
        //        }
        //        if (amount.HasValue && amount != 0)
        //        {
        //            items = items.Where(i => i.Amount == amount);
        //        }
        //        if (!string.IsNullOrEmpty(searchStr))
        //        {
        //            items = items.Where(c => c.Name != null && c.Name.Contains(searchStr.Trim()));
        //        }
        //        if (cityID.HasValue && cityID != 0)
        //        {
        //            items = items.Where(i => i.FranchiseRegionID == cityID);
        //        }
        //        return items = this.Pagination(page, rows, ref total, items, c => c.Id);
        //    }
        //}

       
                if (categoryID.HasValue && categoryID != 0)
                {
                    items = items.Where(i => i.StoreCategoryID == categoryID || i.StoreCategory.ParentID == categoryID);
                }
                if (area.HasValue && area != 0)
                {
                    items = items.Where(i => i.Area == area);
                }
                if (!string.IsNullOrEmpty(searchStr))
                {
                    items = items.Where(c => c.Name != null && c.Name.Contains(searchStr.Trim()));
                }
                //if (cityID.HasValue && cityID != 0)
                //{
                //    items = items.Where(i => i.CityID == cityID);
                //}
                return items;
            }
        }
        /// <summary>
        /// 查询店铺信息
        /// </summary>
        /// <param name="id">店铺id</param>
        /// <returns></returns>
         public store.cyt369.com.models.Store GetStoreByID(long id)
        {
            using(var ctx = this.StoreCtx)
            {
                var store = (from o in ctx.Stores.Include(o => o.StoreImages)
                             .Include(o=>o.Carousels)
                            .Include(o => o.StoreCategory)
                            .ThenInclude(o => o.Parent)
                             where o.Id == id && o.ReviewResult == 1 && o.IsDeleted == false
                             select o).FirstOrDefault();
                                
                return store;
            }
        }
        /// <summary>
        /// 获取店铺扩展信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        public store.cyt369.com.models.StoreExtension GetStoreExtension(int storeId)
        {
            using (var ctx = this.StoreCtx)
            {
                var storeExtension = (from o in ctx.StoreExtensions
                                      where o.StoreID == storeId
                                      select o).FirstOrDefault();
                return storeExtension;
            }
        }
        public IQueryable<store.cyt369.com.models.Store_PropertyEquipment> GetStorePropertyEquipment(int storeId)
        {
            using (var ctx = this.StoreCtx)
            {
                var storePropertyEquipments = (from o in ctx.Store_PropertyEquipments 
                                              where o.StoreID == storeId
                                              select o).ToList().AsQueryable();
                return storePropertyEquipments;
            }
        }
    }
}
